home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / page-ext.el.z / page-ext.el
Text File  |  1998-10-27  |  28KB  |  773 lines

  1. ;;; page-ext.el --- extended page handling commands
  2.  
  3. ;; Copyright (C) 1990, 1991, 1993, 1994 Free Software Foundation
  4.  
  5. ;; Maintainer: Robert J. Chassell <bob@gnu.ai.mit.edu>
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; You may use these commands to handle an address list or other
  27. ;; small data base.
  28.  
  29.  
  30. ;;; Summary
  31.  
  32. ;; The current page commands are:
  33.  
  34. ;;     forward-page         C-x ]
  35. ;;     backward-page        C-x [
  36. ;;     narrow-to-page       C-x p
  37. ;;     count-lines-page     C-x l
  38. ;;     mark-page            C-x C-p  (change this to C-x C-p C-m)
  39. ;;     sort-pages           not bound
  40. ;;     what-page            not bound
  41.  
  42. ;; The new page handling commands all use `C-x C-p' as a prefix.  This
  43. ;; means that the key binding for `mark-page' must be changed.
  44. ;; Otherwise, no other changes are made to the current commands or
  45. ;; their bindings.
  46.  
  47. ;; New page handling commands:
  48.  
  49. ;;     next-page                        C-x C-p C-n
  50. ;;     previous-page                    C-x C-p C-p
  51. ;;     search-pages                     C-x C-p C-s
  52. ;;     add-new-page                     C-x C-p C-a
  53. ;;     sort-pages-buffer                C-x C-p s      
  54. ;;     set-page-delimiter               C-x C-p C-l
  55. ;;     pages-directory                  C-x C-p C-d   
  56. ;;     pages-directory-for-addresses    C-x C-p d
  57. ;;        pages-directory-goto          C-c C-c
  58.  
  59.  
  60. ;;; Using the page commands
  61.  
  62. ;; The page commands are helpful in several different contexts.  For
  63. ;; example, programmers often divide source files into sections using the
  64. ;; `page-delimiter'; you can use the `pages-directory' command to list
  65. ;; the sections.
  66.  
  67. ;; You may change the buffer local value of the `page-delimiter' with
  68. ;; the `set-page-delimiter' command.  This command is bound to `C-x C-p
  69. ;; C-l' The command prompts you for a new value for the page-delimiter.
  70. ;; Called with a prefix-arg, the command resets the value of the
  71. ;; page-delimiter to its original value.
  72.  
  73. ;; You may set several user options:
  74. ;;
  75. ;;   The `pages-directory-buffer-narrowing-p' variable causes the
  76. ;;   `pages-directory-goto' command to narrow to the destination page.
  77. ;;
  78. ;;   The `pages-directory-for-adding-page-narrowing-p' variable, causes the
  79. ;;   `add-new-page' command to narrow to the new entry.
  80. ;;
  81. ;;   The `pages-directory-for-adding-new-page-before-current-page-p' variable
  82. ;;   causes the `add-new-page' command to insert a new page before current
  83. ;;   page.
  84. ;;
  85. ;; These variables are true by default.
  86. ;;
  87. ;; Additional, addresses-related user options are described in the next page
  88. ;; of this file.
  89.  
  90.  
  91. ;;; Handling an address list or small data base
  92.  
  93. ;; You may use the page commands to handle an address list or other
  94. ;; small data base.  Put each address or entry on its own page.  The
  95. ;; first line of text in each page is a `header line' and is listed by
  96. ;; the `pages-directory' or `pages-directory-for-addresses' command.
  97.  
  98. ;; Specifically:
  99. ;; 
  100. ;;   1. Begin each entry with a `page-delimiter' (which is, by default,
  101. ;;      `^L' at the beginning of the line).
  102. ;; 
  103. ;;   2. The first line of text in each entry is the `heading line'; it
  104. ;;      will appear in the pages-directory-buffer which is constructed
  105. ;;      using the `C-x C-p C-d' (pages-directory) command or the `C-x
  106. ;;      C-p d' (pages-directory-for-addresses) command.
  107. ;; 
  108. ;;      The heading line may be on the same line as the page-delimiter
  109. ;;      or it may follow after.  It is the first non-blank line on the
  110. ;;      page.  Conventionally, the heading line is placed on the line
  111. ;;      immediately following the line containing page-delimiter.
  112. ;;
  113. ;;   3. Follow the heading line with the body of the entry.  The body
  114. ;;      extends up to the next `page-delimiter'.  The body may be of any
  115. ;;      length.  It is conventional to place a blank line after the last
  116. ;;      line of the body.
  117.  
  118. ;; For example, a file might look like this:
  119. ;; 
  120. ;;     FSF
  121. ;;     Free Software Foundation
  122. ;;     59 Temple Place - Suite 330
  123. ;;     Boston, MA 02111-1307  USA.
  124. ;;     (617) 542-5942
  125. ;;     gnu@prep.ai.mit.edu
  126. ;; 
  127. ;;     
  128. ;;     House Subcommittee on Intellectual Property,
  129. ;;     U.S. House of Representatives,
  130. ;;     Washington, DC  20515
  131. ;;     
  132. ;;     Congressional committee concerned with permitting or preventing
  133. ;;     monopolistic restrictions on the use of software technology.
  134. ;; 
  135. ;;     
  136. ;;     George Lakoff
  137. ;;     ``Women, Fire, and Dangerous Things:
  138. ;;     What Categories Reveal about the Mind''
  139. ;;     1987, Univ. of Chicago Press
  140. ;; 
  141. ;;     About philosophy, Whorfian effects, and linguistics.
  142. ;; 
  143. ;;       
  144. ;;     OBI   (On line text collection.)
  145. ;;     Open Book Initiative
  146. ;;     c/o Software Tool & Die
  147. ;;     1330 Beacon St, Brookline, MA 02146 USA
  148. ;;     (617) 739-0202 
  149. ;;     obi@world.std.com
  150.  
  151. ;; In this example, the heading lines are:
  152. ;;
  153. ;;     FSF
  154. ;;     House Subcommittee on Intellectual Property
  155. ;;     George Lakoff
  156. ;;     OBI (On line text collection.)
  157.  
  158. ;; The `C-x C-p s' (sort-pages-buffer) command sorts the entries in the
  159. ;; buffer alphabetically.
  160.  
  161. ;; You may use any of the page commands, including the `next-page',
  162. ;; `previous-page', `add-new-page', `mark-page', and `search-pages'
  163. ;; commands.
  164.  
  165. ;; You may use either the `C-x C-p d' (pages-directory-for-addresses)
  166. ;; or the `C-x C-p C-d' (pages-directory) command to construct and
  167. ;; display a directory of all the heading lines.
  168.  
  169. ;; In the directory, you may position the cursor over a heading line
  170. ;; and type `C-c C-c' (pages-directory-goto) to go to the entry to
  171. ;; which it refers in the pages buffer.
  172.  
  173. ;; You can type `C-c C-p C-a' (add-new-page) to add a new entry in the
  174. ;; pages buffer or address file.  This is the same command you use to
  175. ;; add a new entry when you are in the pages buffer or address file.
  176.  
  177. ;; If you wish, you may create several different directories,
  178. ;; one for each different buffer.
  179.  
  180. ;; `pages-directory-for-addresses' in detail
  181.  
  182. ;; The `pages-directory-for-addresses' assumes a default addresses
  183. ;; file.  You do not need to specify the addresses file but merely type
  184. ;; `C-x C-p d' from any buffer.  The command finds the file, constructs
  185. ;; a directory for it, and switches you to the directory.  If you call
  186. ;; the command with a prefix arg, `C-u C-x C-p d', it prompts you for a
  187. ;; file name.
  188.  
  189. ;; You may customize the addresses commands:
  190.  
  191. ;;   The `pages-addresses-file-name' variable determines the name of
  192. ;;   the addresses file; by default it is "~/addresses".
  193.  
  194. ;;   The `pages-directory-for-addresses-goto-narrowing-p' variable
  195. ;;   determines whether `pages-directory-goto' narrows the addresses
  196. ;;   buffer to the entry, which it does by default.
  197.  
  198. ;;   The `pages-directory-for-addresses-buffer-keep-windows-p' variable
  199. ;;   determines whether `pages-directory-for-addresses' deletes other
  200. ;;   windows to show as many lines as possible on the screen or works
  201. ;;   in the usual Emacs manner and keeps other windows.  Default is to
  202. ;;   keep other windows.
  203.  
  204. ;;   The `pages-directory-for-adding-addresses-narrowing-p' variable
  205. ;;   determines whether `pages-directory-for-addresses' narrows the
  206. ;;   addresses buffer to a new entry when you are adding that entry.
  207. ;;   Default is to narrow to new entry, which means you see a blank
  208. ;;   screen before you write the new entry.
  209.  
  210. ;; `pages-directory' in detail
  211.  
  212. ;; Call the `pages-directory' command from the buffer for which you
  213. ;; want a directory created; it creates a directory for the buffer and
  214. ;; pops you to the directory.
  215.  
  216. ;; The `pages-directory' command has several options:
  217.  
  218. ;;   Called with a prefix arg, `C-u C-x C-p C-d', the `pages-directory'
  219. ;;   prompts you for a regular expression and only lists only those
  220. ;;   header lines that are part of pages that contain matches to the
  221. ;;   regexp.  In the example above, `C-u C-x C-p C-d 617 RET' would
  222. ;;   match the telephone area code of the first and fourth entries, so
  223. ;;   only the header lines of those two entries would appear in the
  224. ;;   pages-directory-buffer.
  225. ;; 
  226. ;;   Called with a numeric argument, the `pages-directory' command
  227. ;;   lists the number of lines in each page.  This is helpful when you
  228. ;;   are printing hardcopy.  
  229.  
  230. ;;   Called with a negative numeric argument, the `pages-directory'
  231. ;;   command lists the lengths of pages whose contents match a regexp.
  232.  
  233. ;;; Code:
  234.  
  235.  
  236. ;;; Customarily customizable variable definitions
  237.  
  238. (defvar pages-directory-buffer-narrowing-p t
  239.   "*If non-nil, `pages-directory-goto' narrows pages buffer to entry.")
  240.  
  241. (defvar pages-directory-for-adding-page-narrowing-p t
  242.   "*If non-nil, `add-new-page' narrows page buffer to new entry.")
  243.  
  244. (defvar pages-directory-for-adding-new-page-before-current-page-p t
  245.   "*If non-nil, `add-new-page' inserts new page before current page.")
  246.  
  247.  
  248. ;;; Addresses related variables
  249.  
  250. (defvar pages-addresses-file-name "~/addresses"
  251.   "*Standard name for file of addresses. Entries separated by page-delimiter.
  252. Used by `pages-directory-for-addresses' function.")
  253.  
  254. (defvar pages-directory-for-addresses-goto-narrowing-p t
  255.   "*If non-nil, `pages-directory-goto' narrows addresses buffer to entry.")
  256.  
  257. (defvar pages-directory-for-addresses-buffer-keep-windows-p t
  258.   "*If nil, `pages-directory-for-addresses' deletes other windows.")
  259.  
  260. (defvar pages-directory-for-adding-addresses-narrowing-p t
  261.   "*If non-nil, `add-new-page' narrows addresses buffer to new entry.")
  262.  
  263.  
  264. ;;; Key bindings for page handling functions
  265.  
  266. (global-unset-key "\C-x\C-p")
  267.  
  268. (defvar ctl-x-ctl-p-map (make-sparse-keymap)
  269.   "Keymap for subcommands of C-x C-p, which are for page handling.")
  270.  
  271. (define-key ctl-x-map "\C-p" 'ctl-x-ctl-p-prefix)
  272. (fset 'ctl-x-ctl-p-prefix ctl-x-ctl-p-map)
  273.  
  274. (define-key ctl-x-ctl-p-map "\C-n" 'next-page)
  275. (define-key ctl-x-ctl-p-map "\C-p" 'previous-page)
  276. (define-key ctl-x-ctl-p-map "\C-a" 'add-new-page)
  277. (define-key ctl-x-ctl-p-map "\C-m" 'mark-page)
  278. (define-key ctl-x-ctl-p-map "\C-s" 'search-pages)
  279. (define-key ctl-x-ctl-p-map "s"    'sort-pages-buffer)
  280. (define-key ctl-x-ctl-p-map "\C-l" 'set-page-delimiter)
  281. (define-key ctl-x-ctl-p-map "\C-d" 'pages-directory)
  282. (define-key ctl-x-ctl-p-map "d"    'pages-directory-for-addresses)
  283.  
  284.  
  285. ;;; Page movement function definitions
  286.  
  287. (defun next-page (&optional count)
  288.   "Move to the next page bounded by the `page-delimiter' variable.
  289. With arg (prefix if interactive), move that many pages."
  290.   (interactive "p")
  291.   (or count (setq count 1))
  292.   (widen)
  293.   ;; Cannot use forward-page because of problems at page boundaries.
  294.   (while (and (> count 0) (not (eobp)))
  295.     (if (re-search-forward page-delimiter nil t)
  296.         nil
  297.       (goto-char (point-max)))
  298.     (setq count (1- count)))
  299.   ;; If COUNT is negative, we want to go back -COUNT + 1 page boundaries.
  300.   ;; The first page boundary we reach is the top of the current page,
  301.   ;; which doesn't count.
  302.   (while (and (< count 1) (not (bobp)))
  303.     (if (re-search-backward page-delimiter nil t)
  304.     (goto-char (match-beginning 0))
  305.       (goto-char (point-min)))
  306.     (setq count (1+ count)))
  307.   (narrow-to-page)
  308.   (goto-char (point-min))
  309.   (recenter 0))
  310.  
  311. (defun previous-page (&optional count)
  312.   "Move to the previous page bounded by the `page-delimiter' variable.
  313. With arg (prefix if interactive), move that many pages."
  314.   (interactive "p")
  315.   (or count (setq count 1))
  316.   (next-page (- count)))
  317.  
  318.  
  319. ;;; Adding and searching pages
  320.  
  321. (defun add-new-page (header-line)
  322.   "Insert new page.  Prompt for header line.
  323.  
  324. If point is in the pages directory buffer, insert the new page in the
  325. buffer associated with the directory.
  326.  
  327. Insert the new page just before current page if
  328.   pages-directory-for-adding-new-page-before-current-page-p  variable
  329. is non-nil.  Else insert at exact location of point.
  330.  
  331. Narrow to new page if
  332.   pages-directory-for-adding-page-narrowing-p variable
  333. is non-nil.
  334.  
  335. Page begins with a `^L' as the default page-delimiter.  
  336. Use \\[set-page-delimiter] to change the page-delimiter.
  337. Point is left in the body of page."
  338.   (interactive "sHeader line: ")
  339.   (widen)
  340.   ;; If in pages directory buffer
  341.   (if (eq major-mode 'pages-directory-mode) 
  342.       (progn
  343.         ;; Add new page before or after current page?
  344.         (if pages-directory-for-adding-new-page-before-current-page-p
  345.             (pages-directory-goto)
  346.           (pages-directory-goto)
  347.           (forward-page)
  348.           (or (eobp) (forward-line -1)))))
  349.   (widen)
  350.   ;; Move point before current delimiter if desired.
  351.   (and pages-directory-for-adding-new-page-before-current-page-p
  352.        (if (re-search-backward page-delimiter nil t)
  353.            (goto-char (match-beginning 0))
  354.          ;; If going to beginning of file, insert a page-delimiter
  355.          ;; before current first page.
  356.          (goto-char (point-min))
  357.          (insert
  358.           (format "%s\n"
  359.                   ;; Remove leading `^' from page-delimiter string
  360.                   (if (eq '^ (car (read-from-string page-delimiter)))
  361.                       (substring page-delimiter 1))))         
  362.          (goto-char (point-min))))
  363.   ;; Insert page delimiter at beginning of line.
  364.   (if (not (looking-at "^."))   (forward-line 1))
  365.   (insert (format "%s\n%s\n\n\n" 
  366.                   (if (eq '^ (car (read-from-string page-delimiter)))
  367.                       (substring page-delimiter 1))
  368.                   header-line))
  369.   (forward-line -1)
  370.   (and pages-directory-for-adding-page-narrowing-p (narrow-to-page)))
  371.  
  372. (defvar pages-last-search nil
  373.   "Value of last regexp searched for.  Initially, nil.")
  374.  
  375. (defun search-pages (regexp)
  376.   "Search for REGEXP, starting from point, and narrow to page it is in."
  377.   (interactive (list
  378.                 (read-string
  379.                  (format "Search for `%s' (end with RET): "
  380.                          (or pages-last-search "regexp")))))
  381.   (if (equal regexp "")
  382.       (setq regexp pages-last-search)
  383.     (setq pages-last-search regexp))
  384.   (widen)
  385.   (re-search-forward regexp)
  386.   (narrow-to-page))
  387.  
  388.  
  389. ;;; Sorting pages
  390.  
  391. (autoload 'sort-subr "sort" "Primary function for sorting." t nil)
  392.  
  393. (defun sort-pages-in-region (reverse beg end)
  394.   "Sort pages in region alphabetically.  Prefix arg means reverse order.  
  395.  
  396. Called from a program, there are three arguments:
  397. REVERSE (non-nil means reverse order), BEG and END (region to sort)."
  398.  
  399. ;;; This sort function handles ends of pages differently than
  400. ;;; `sort-pages' and works better with lists of addresses and similar
  401. ;;; files.
  402.  
  403.   (interactive "P\nr")
  404.   (save-restriction
  405.     (narrow-to-region beg end)
  406.     (goto-char (point-min))
  407.     ;;; `sort-subr' takes three arguments
  408.     (sort-subr reverse
  409.  
  410.                ;; NEXTRECFUN is called with point at the end of the
  411.                ;; previous record. It moves point to the start of the
  412.                ;; next record.
  413.            (function (lambda ()
  414.                            (re-search-forward page-delimiter nil t)
  415.                            (skip-chars-forward " \t\n")
  416.                            ))
  417.  
  418.                ;; ENDRECFUN is is called with point within the record.
  419.                ;; It should move point to the end of the record.
  420.            (function (lambda ()
  421.                            (if (re-search-forward
  422.                                 page-delimiter
  423.                                 nil
  424.                                 t)
  425.                                (goto-char (match-beginning 0))
  426.                              (goto-char (point-max))))))))
  427.  
  428. (defun sort-pages-buffer (&optional reverse)
  429.   "Sort pages alphabetically in buffer.  Prefix arg means reverse order. 
  430. \(Non-nil arg if not interactive.\)"
  431.  
  432.   (interactive "P")
  433.   (or reverse (setq reverse nil))
  434.   (widen)
  435.   (let ((beginning (point-min))
  436.         (end (point-max)))
  437.     (sort-pages-in-region reverse beginning end)))
  438.  
  439.  
  440. ;;; Pages directory ancillary definitions
  441.  
  442. (defvar pages-directory-previous-regexp nil
  443.   "Value of previous regexp used by `pages-directory'.
  444. \(This regular expression may be used to select only those pages that
  445. contain matches to the regexp.\)")
  446.  
  447. (defvar pages-buffer nil
  448.   "The buffer for which the pages-directory function creates the directory.")
  449.  
  450. (defvar pages-directory-prefix "*Directory for:"
  451.   "Prefix of name of temporary buffer for pages-directory.")
  452.  
  453. (defvar pages-pos-list nil
  454.   "List containing the positions of the pages in the pages-buffer.")
  455.  
  456. (defvar pages-directory-map nil
  457.   "Keymap for the pages-directory-buffer.")
  458.  
  459. (if pages-directory-map
  460.     ()
  461.   (setq pages-directory-map (make-sparse-keymap))
  462.   (define-key pages-directory-map "\C-c\C-c"
  463.     'pages-directory-goto)
  464.   (define-key pages-directory-map "\C-c\C-p\C-a" 'add-new-page))
  465.  
  466. (defvar original-page-delimiter "^\f"
  467.   "Default page delimiter.")
  468.  
  469. (defun set-page-delimiter (regexp reset-p)
  470.   "Set buffer local value of page-delimiter to REGEXP.
  471. Called interactively with a prefix argument, reset `page-delimiter' to
  472. its original value.
  473.  
  474. In a program, non-nil second arg causes first arg to be ignored and
  475. resets the page-delimiter to the original value."
  476.   
  477.   (interactive
  478.    (if current-prefix-arg
  479.        (list original-page-delimiter "^\f")
  480.      (list (read-string "Set page-delimiter to regexp: " page-delimiter)
  481.            nil)))
  482.   (make-local-variable 'original-page-delimiter)
  483.   (make-local-variable 'page-delimiter)
  484.   (setq original-page-delimiter
  485.         (or original-page-delimiter page-delimiter))
  486.   (if (not reset-p)
  487.       (setq page-delimiter regexp)
  488.     (setq page-delimiter original-page-delimiter))
  489.   (if (interactive-p)
  490.       (message "The value of `page-delimiter' is now: %s" page-delimiter)))
  491.  
  492.  
  493. ;;; Pages directory main definitions
  494.  
  495. (defun pages-directory
  496.   (pages-list-all-headers-p count-lines-p &optional regexp)
  497.   "Display a directory of the page headers in a temporary buffer.
  498. A header is the first non-blank line after the page-delimiter.
  499. \\[pages-directory-mode]
  500. You may move point to one of the lines in the temporary buffer,
  501. then use \\<pages-directory-goto> to go to the same line in the pages buffer.
  502.  
  503. In interactive use:
  504.  
  505.     1. With no prefix arg, display all headers.
  506.  
  507.     2. With prefix arg, display the headers of only those pages that
  508.        contain matches to a regular expression for which you are
  509.        prompted.
  510.  
  511.     3. With numeric prefix arg, for every page, print the number of
  512.        lines within each page.
  513.  
  514.     4. With negative numeric prefix arg, for only those pages that
  515.        match a regular expression, print the number of lines within
  516.        each page.
  517.  
  518. When called from a program, non-nil first arg means list all headers;
  519. non-nil second arg means print numbers of lines in each page; if first
  520. arg is nil, optional third arg is regular expression.
  521.  
  522. If the buffer is narrowed, the `pages-directory' command creates a
  523. directory for only the accessible portion of the buffer."
  524.   
  525.   (interactive
  526.    (cond ((not current-prefix-arg)
  527.           (list t nil nil))
  528.          ((listp current-prefix-arg) 
  529.           (list nil
  530.                 nil
  531.                 (read-string
  532.                  (format "Select according to `%s' (end with RET): "
  533.                          (or pages-directory-previous-regexp "regexp")))))
  534.          ((> (prefix-numeric-value current-prefix-arg) 0)
  535.           (list t t nil))
  536.          ((< (prefix-numeric-value current-prefix-arg) 0)          
  537.           (list nil
  538.                 t
  539.                 (read-string
  540.                  (format "Select according to `%s' (end with RET): "
  541.                          (or pages-directory-previous-regexp "regexp")))))))
  542.   
  543.   (if (equal regexp "")
  544.       (setq regexp pages-directory-previous-regexp)
  545.     (setq pages-directory-previous-regexp regexp))
  546.   
  547.   (if (interactive-p)
  548.       (message "Creating directory for: %s "
  549.                (buffer-name)))
  550.   
  551.   (let ((target-buffer (current-buffer))
  552.         (pages-directory-buffer
  553.      (concat pages-directory-prefix " " (buffer-name)))
  554.         (linenum 1)
  555.         (pages-buffer-original-position (point))
  556.         (pages-buffer-original-page 0))
  557.     
  558.     ;; `with-output-to-temp-buffer' binds the value of the variable
  559.     ;; `standard-output' to the buffer named as its first argument,
  560.     ;; but does not switch to that buffer.
  561.     (with-output-to-temp-buffer pages-directory-buffer
  562.       (save-excursion
  563.         (set-buffer standard-output)
  564.         (pages-directory-mode)
  565.         (insert
  566.          "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n)
  567.         (setq pages-buffer target-buffer)
  568.         (setq pages-pos-list nil))
  569.       
  570.       (if pages-list-all-headers-p
  571.           
  572.           ;; 1. If no prefix argument, list all headers
  573.           (save-excursion
  574.             (goto-char (point-min))
  575.             
  576.             ;; (a) Point is at beginning of buffer; but the first
  577.             ;;     page may not begin with a page-delimiter
  578.             (save-restriction
  579.               ;; If page delimiter is at beginning of buffer, skip it
  580.               (if (and (save-excursion
  581.                          (re-search-forward page-delimiter nil t))
  582.                        (= 1 (match-beginning 0)))
  583.                   (goto-char (match-end 0)))
  584.               (narrow-to-page)
  585.               (pages-copy-header-and-position count-lines-p))
  586.             
  587.             ;; (b) Search within pages buffer for next page-delimiter
  588.             (while (re-search-forward page-delimiter nil t)
  589.               (pages-copy-header-and-position count-lines-p)))
  590.         
  591.         ;; 2. Else list headers whose pages match regexp.
  592.         (save-excursion
  593.           ;; REMOVED  save-restriction  AND  widen  FROM HERE
  594.           (goto-char (point-min))
  595.           
  596.           ;; (a) Handle first page
  597.           (save-restriction
  598.             (narrow-to-page)
  599.             ;; search for selection regexp
  600.             (if (save-excursion (re-search-forward regexp nil t))
  601.                 (pages-copy-header-and-position count-lines-p)))
  602.           
  603.           ;; (b) Search for next page-delimiter
  604.           (while (re-search-forward page-delimiter nil t)
  605.             (save-restriction
  606.               (narrow-to-page)
  607.               ;; search for selection regexp
  608.               (if (save-excursion (re-search-forward regexp nil t))
  609.                   (pages-copy-header-and-position count-lines-p)
  610.                 )))))
  611.       
  612.       (set-buffer standard-output)
  613.       ;; Put positions in increasing order to go with buffer.
  614.       (setq pages-pos-list (nreverse pages-pos-list))
  615.       (if (interactive-p)
  616.           (message "%d matching lines in: %s"
  617.                    (length pages-pos-list) (buffer-name target-buffer))))
  618.     (pop-to-buffer pages-directory-buffer)
  619.     (sit-for 0)  ; otherwise forward-line fails if N > window height.
  620.     (forward-line (if (= 0 pages-buffer-original-page)
  621.                       1
  622.                     pages-buffer-original-page))))
  623.  
  624. (defun pages-copy-header-and-position (count-lines-p)
  625.   "Copy page header and its position to the Pages Directory.
  626. Only arg non-nil, count lines in page and insert before header.
  627. Used by `pages-directory' function."
  628.   
  629.   (let (position line-count)
  630.  
  631.     (if count-lines-p
  632.         (save-excursion
  633.           (save-restriction
  634.             (narrow-to-page)
  635.             (setq line-count (count-lines (point-min) (point-max))))))
  636.  
  637.     ;; Keep track of page for later cursor positioning
  638.     (if (<= (point) pages-buffer-original-position)
  639.         (setq pages-buffer-original-page
  640.               (1+ pages-buffer-original-page)))
  641.     
  642.     (save-excursion
  643.       ;; go to first non-blank char after the page-delimiter
  644.       (skip-chars-forward " \t\n")     
  645.       ;; set the marker here; this the place to which the
  646.       ;; `pages-directory-goto' command will go 
  647.       (setq position (make-marker))
  648.       (set-marker position (point))
  649.       (let ((start (point))
  650.             (end (save-excursion (end-of-line) (point))))
  651.         ;; change to directory buffer
  652.         (set-buffer standard-output)
  653.         ;; record page position 
  654.         (setq pages-pos-list (cons position pages-pos-list))
  655.         ;; insert page header
  656.         (insert-buffer-substring target-buffer start end))
  657.       
  658.       (if count-lines-p
  659.           (save-excursion
  660.             (beginning-of-line)
  661.             (insert (format "%3d: " line-count))))
  662.       
  663.       (terpri))
  664.     (end-of-line 1)))
  665.  
  666. (defun pages-directory-mode ()
  667.   "Mode for handling the pages-directory buffer.
  668.  
  669. Move point to one of the lines in this buffer, then use \\[pages-directory-goto] to go
  670. to the same line in the pages buffer."
  671.  
  672.   (kill-all-local-variables)
  673.   (use-local-map pages-directory-map)
  674.   (setq major-mode 'pages-directory-mode)
  675.   (setq mode-name "Pages-Directory")
  676.   (make-local-variable 'pages-buffer)
  677.   (make-local-variable 'pages-pos-list)
  678.   (make-local-variable 'pages-directory-buffer-narrowing-p))
  679.  
  680. (defun pages-directory-goto ()
  681.   "Go to the corresponding line in the pages buffer."
  682.  
  683. ;;; This function is mostly a copy of `occur-mode-goto-occurrence'
  684.  
  685.   (interactive)
  686.   (if (or (not pages-buffer)
  687.       (not (buffer-name pages-buffer)))
  688.       (progn
  689.     (setq pages-buffer nil
  690.           pages-pos-list nil)
  691.     (error "Buffer in which pages were found is deleted.")))
  692.   (beginning-of-line)
  693.   (let* ((pages-number (1- (count-lines (point-min) (point))))
  694.      (pos (nth pages-number pages-pos-list))
  695.          (end-of-directory-p (eobp))
  696.          (narrowing-p  pages-directory-buffer-narrowing-p))
  697.     (pop-to-buffer pages-buffer)  
  698.     (widen)
  699.     (if end-of-directory-p
  700.         (goto-char (point-max))
  701.       (goto-char (marker-position pos)))
  702.     (if narrowing-p (narrow-to-page))))
  703.  
  704.  
  705. ;;; The `pages-directory-for-addresses' function and ancillary code
  706.  
  707. (defun pages-directory-for-addresses (&optional filename)
  708.   "Find addresses file and display its directory.
  709. By default, create and display directory of `pages-addresses-file-name'.
  710. Optional argument is FILENAME.  In interactive use, with prefix
  711. argument, prompt for file name and provide completion.
  712.  
  713. Move point to one of the lines in the displayed directory,
  714. then use \\[pages-directory-goto] to go to the same line
  715. in the addresses buffer.
  716.  
  717. If    pages-directory-for-addresses-goto-narrowing-p    is non-nil,
  718. `pages-directory-goto' narrows addresses buffer to entry.
  719.  
  720. If    pages-directory-for-addresses-buffer-keep-windows-p     is nil,
  721. this command deletes other windows when it displays the addresses 
  722. directory."
  723.  
  724.   (interactive
  725.    (list (if current-prefix-arg
  726.              (read-file-name "Filename: " pages-addresses-file-name))))
  727.  
  728.   (if (interactive-p)
  729.       (message "Creating directory for: %s "
  730.                (or filename pages-addresses-file-name)))
  731.   (if (file-exists-p (or filename pages-addresses-file-name))
  732.       (progn
  733.         (set-buffer
  734.          (find-file-noselect
  735.           (expand-file-name
  736.            (or filename pages-addresses-file-name))))
  737.         (widen)
  738.         (pages-directory t nil nil)
  739.         (pages-directory-address-mode)
  740.         (setq pages-directory-buffer-narrowing-p
  741.               pages-directory-for-addresses-goto-narrowing-p)
  742.         (or pages-directory-for-addresses-buffer-keep-windows-p
  743.             (delete-other-windows))
  744.         (save-excursion
  745.           (goto-char (point-min))
  746.           (delete-region (point) (save-excursion (end-of-line) (point)))
  747.           (insert
  748.            "=== Address List Directory: use `C-c C-c' to go to page under cursor. ===")
  749.           (set-buffer-modified-p nil)
  750.           ))
  751.     (error "No addresses file found!")))
  752.  
  753. (defun pages-directory-address-mode ()
  754.   "Mode for handling the Addresses Directory buffer.
  755.  
  756. Move point to one of the lines in this buffer,
  757. then use \\[pages-directory-goto] to go
  758. to the same line in the pages buffer."
  759.  
  760.   (use-local-map pages-directory-map)
  761.   (setq major-mode 'pages-directory-address-mode)
  762.   (setq mode-name "Addresses Directory")
  763.   (make-local-variable 'pages-buffer)
  764.   (make-local-variable 'pages-pos-list)
  765.   (make-local-variable 'pages-directory-buffer-narrowing-p))
  766.  
  767.  
  768. ;;; Place `provide' at end of file.
  769. (provide 'page-ext)
  770.  
  771. ;;;;;;;;;;;;;;;; end of page-ext.el ;;;;;;;;;;;;;;;;
  772.  
  773.